<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

class UnionLoginPhoneEncryptCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'union_login_phone_encrypt';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '联合登录手机号加密';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        try{
            \DB::beginTransaction();
            $users = User::get();
            foreach ($users as $user){
                $user->update([
                    'phone_num_sha' => hash('SHA256',$user->phone_num),
                    'phone_num_md5' => md5($user->phone_num),
                ]);
            }
            \DB::commit();
        }catch (\Exception $e){
            \DB::rollBack();
            \Log::debug($e->getMessage());
        }
    }
}
